package com.sc.web.member;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.ParamMap;
import com.sc.dal.model.member.Member;
import com.sc.dal.model.member.MemberGrade;
import com.sc.dal.model.system.UserFormMap;
import com.sc.service.member.MemberGradeService;
import com.sc.service.member.MemberService;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.data.MD5Util;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;
import com.sc.web.plugin.PageView;

@Controller
@RequestMapping(value="/grade")
public class MemberGradeController extends BaseController{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberGradeService memberGradeService;
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,Model model){
		//model.addAttribute("discount", discountService.selectList());
		return "member/grade/list";
	}
	
	@RequestMapping(value="/selectList")
	@ResponseBody
	public PageView selectList(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		try{
			ParamMap paramMap = getFormMap(ParamMap.class);		
			paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
			paramMap.put("column", column);
			paramMap.put("sort", sort);
			List<MemberGrade> memberList = this.memberGradeService.selectListByPage(paramMap);
			pageView.setRecords(memberList);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			logger.info("查询会员等级成功!");
		}catch(Exception e){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("查询会员等级异常:"+e);
			throw new ScBizException("查询会员数据异常！", e);
		}
		
		return pageView;
	}
	
	@RequestMapping(value="/addOrEdit")
	public  String addaddOrEdit(String id,Model model){
		try {
			Integer	idInt=Integer.parseInt(id);
			MemberGrade memberGrade=memberGradeService.selectByPrimaryKey(idInt);
			model.addAttribute("memberGrade", memberGrade);
			return "/member/grade/edit";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			return "/member/grade/edit";
		}
	}
	@RequestMapping(value="/saveGrade")
	@ResponseBody
	public String saveGrade(HttpServletRequest request,MemberGrade memberGrade){
		try {
			Integer id=memberGrade.getId();
			MemberGrade memberGradeSel=null;
			UserFormMap userFormMap=(UserFormMap) SecurityUtils.getSubject().getSession().getAttribute("userSession");
			if(id==null||"".equals(id)){
				memberGradeSel=memberGradeService.selectByGrade(memberGrade);
				if(memberGradeSel!=null){
					return "会员等级已存在！";
				}
				memberGrade.setCreater(userFormMap.getStr("accountName"));
				memberGrade.setCreateTime(new Date());
				memberGradeService.insertSelective(memberGrade);
				return "\"success\"";
			}else{
				memberGrade.setModifier(userFormMap.getStr("accountName"));
				memberGrade.setModifyTime(new Date());
				memberGradeService.updateByPrimaryKeySelective(memberGrade);
				return "\"success\"";
			}
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
	@ResponseBody
	@RequestMapping("deleteEntity")
	public String deleteEntity(){
		try {
			UserFormMap userFormMap=(UserFormMap) SecurityUtils.getSubject().getSession().getAttribute("userSession");
			String ids = getPara("ids");
			String[] idArr=ids.split(",");
			for (String string : idArr) {
				MemberGrade memberGrade=new MemberGrade();
				memberGrade.setId(Integer.parseInt(string));
				memberGrade.setIsDelete(Constants.IS_DELETEB);
				memberGrade.setModifier(userFormMap.getStr("accountName"));
				memberGrade.setModifyTime(new Date());
				memberGradeService.updateByPrimaryKeySelective(memberGrade);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
}
